﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Newtonsoft.Json;
using QD.Utils;

namespace QD.Web
{
    public class WebPage : System.Web.UI.Page
    {

        public Parse Parse { get; private set; }

        protected TimeSpan TimeCost { get; private set; }

        public WebPage()
        {

        }
        public override void ProcessRequest(HttpContext context)
        {
            Parse = new Parse(context.Request);
            base.ProcessRequest(context);

        }

        protected override void OnLoad(EventArgs e)
        {

            base.OnLoad(e);
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            TimeCost = DateTime.Now - HttpContext.Current.Timestamp;
        }


        public void JsonResult(int code, string message, object item = null)
        {

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{{\"code\":{0},\"msg\":{1}", code, JsonConvert.SerializeObject(message));
            if (item != null)
                sb.AppendFormat(",\"item\":{0}", JsonConvert.SerializeObject(item));
            sb.Append("}");
            Context.Response.Clear();
            Context.Response.Write(sb.ToString());
            Context.Response.End();

        }

        public void Redirect(string path)
        {
            this.Response.Redirect(path, true);
        }

    }
}
